#include "mclient.h"
#include "ui_mclient.h"

QString mClient::str;

mClient::mClient(QWidget *parent) :
    QMainWindow(parent),
   // m_adv(new advertising(this)),
    m_pay(new payment(this)),
    ui(new Ui::mClient)
{
    ui->setupUi(this);
    connect(m_pay, &payment::new_buy, this, &mClient::send_sell_num);

}

mClient::~mClient()
{
    delete ui;
}

void mClient::send_sell_num(int sell_num)
{
    qDebug()<<"已卖出的商品"<<sell_num;
    QString text = QString::number(sell_num);
    qDebug() << text;
    socket->write(text.toUtf8().data());

}

void mClient::on_host_connect_bt_clicked()
{
    QString ip=ui->host_ip_le->text();
    int port=ui->host_port_le->text().toInt();
    name=ui->host_name_le->text();
    qDebug()<<"ip"<<ip;
    qDebug()<<"port:"<<port;
    qDebug()<<"name"<<name;

    socket=new QTcpSocket;
    connect(socket,SIGNAL(connected()),this,SLOT(connect_host_success()));
    socket->connectToHost(ip,port);



    connect(socket,&QTcpSocket::readyRead,this,[this](){
       //获取通信套接字的内容
        QByteArray data = socket->readAll();
        mClient::str = QString::fromUtf8(data); // 假设服务器发送的是UTF-8编码的数据
        qDebug()<<"receive:"<<mClient::str;

    });
   // m_adv->equipment_num_label=equipment_num;



}

void mClient::connect_host_success(void)
{
    ui->status_lb->setText("success to connect server");
    qDebug()<<"finish connect";

         advertising *new_board=new advertising(this);
       //new_board->ui->equipment_num=equipment_num;
        new_board->equipment_num_final=equipment_num;
        new_board->show();
        this->hide();

        QString text = name;
        qDebug() << text;
        socket->write(text.toUtf8().data());



}



